package com.ruoyi.web.controller.common;

import com.ruoyi.common.config.ServerConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.service.AdminUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.Map;

/**
 * 通用请求处理
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/common/admin/upload")
public class AdminUploadController
{

    @Autowired
    private ServerConfig serverConfig;


    @Autowired
    private AdminUploadService adminUploadService;

    /**
     * 通用上传请求（单个）
     */
    @PostMapping("/up/single")
    @ResponseBody
    public AjaxResult uploadFile(MultipartFile file) throws Exception
    {
        try
        {


            Map<String, String> stringStringMap = adminUploadService.uploadFile(file);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", stringStringMap.get("fileName"));
            ajax.put("url", stringStringMap.get("url"));
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 多文件上传
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/up/more")
    @ResponseBody
    public AjaxResult uploadFileToMore(MultipartHttpServletRequest request) throws Exception
    {
        try
        {
            AjaxResult ajax = AjaxResult.success();
            Map<String, MultipartFile> fileMap = request.getFileMap();
            MultiValueMap<String, MultipartFile> filesMap = request.getMultiFileMap();
            for(Map.Entry<String, List<MultipartFile>> entry:filesMap.entrySet()){
                // 上传并返回新文件名称
                List<MultipartFile> value = entry.getValue();
                StringBuffer sb=new StringBuffer();
                if(value.size()>1){
                    value.stream().forEach( a->{
                        Map<String, String> stringStringMap = adminUploadService.uploadFile( a);
                        sb.append(stringStringMap.get("url")).append(";");
                    });
                }else{
                    Map<String, String> stringStringMap = adminUploadService.uploadFile( entry.getValue().get(0));
                    sb.append(stringStringMap.get("url"));
                }

                ajax.put(entry.getKey()+"_url", sb.toString());
            }

            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    public static void main(String[] args) {
        String filename="asdjasdjaasd.app";
        String substring = filename.substring(filename.lastIndexOf("."));

        StringBuffer sb =new StringBuffer("");
        sb.append(LocalDateTime.now().toInstant(ZoneOffset.ofHours(+8)).toEpochMilli());
        sb.append(UUID.randomUUID());
        sb.append(substring);
        System.out.println(sb.toString());}

}
